package com.music.service.urls.dao.impl;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.music.service.jdosupport.PMF;
import com.music.service.urls.beans.Url;
import com.music.service.urls.constants.UrlState;
import com.music.service.urls.dao.UrlDAO;

public class UrlJDOImpl implements UrlDAO {

	public List<Url> getUrlsOfSong(Long pSongId) {
		PersistenceManager persistenceManager = PMF.get()
		.getPersistenceManager();

		Query query = persistenceManager.newQuery(Url.class);
		query.setFilter("songId == songIdParam");
		query.setFilter("state == urlStateParam");
		query.declareParameters("Long songIdParam");
		query.declareParameters("enum urlStateParam");
		List<Url> results;
		results = (List<Url>) query.execute(pSongId,UrlState.ACTIVE);
		
		return results;
	}
	

	
	
	public Url addNewUrl(Url pUrl) {
		pUrl.setState(UrlState.ACTIVE);
		
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			pUrl = persistenceManager.makePersistent(pUrl);
		} finally {
			persistenceManager.close();
		}
		return pUrl;
	}

	public List<Url> getUrls(Set<Long> pUrlIds){
		PersistenceManager persistenceManager = PMF.get()
		.getPersistenceManager();

		Query query = persistenceManager.newQuery(Url.class,
		":p.contains(urlId)");

		List<Url> urls = (List<Url>) query.execute(Arrays.asList(pUrlIds));
		return urls;
	}
	


		

	public void deleteUrl(Url pUrl) {
		pUrl.setState(UrlState.DELETED);
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			persistenceManager.makePersistent(pUrl);
		} finally {
			persistenceManager.close();
		}
	}

	public Url editUrl(Url pUrl){
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			persistenceManager.makePersistent(pUrl);
		} finally {
			persistenceManager.close();
		}
		return pUrl;
	}



}
